/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.siseventos.util;

import java.io.InputStream;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;

/**
 *
 * @author Ivan
 */
public class MailHelper {

    // Campos
    private static Properties props;
    private static String hostname;
    private static String username;
    private static String password;
    private static String port;
    private static String from;
    private static Session session;
    private static Logger log;

    static {
        // Buscando as configurações
        setupConfig("mail.properties");

        // Buscando o usuário e senha
        username = props.getProperty("username");
        password = props.getProperty("password");
        hostname = props.getProperty("hostname");
        port = props.getProperty("port");

        // Configurando a sessão
        session = Session.getInstance(props, new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        log = Logger.getLogger(MailHelper.class);
    }

    // Serviço
    public static void sendSimpleMail(String to, String subject, String message) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setHostName(hostname);
            email.setAuthenticator(new DefaultAuthenticator(username,
            password));
            email.setSmtpPort(465);
            email.setTLS(true);
            email.addTo(to, to);
            email.setFrom(from, "SisEventos");
            email.setSubject(subject);
            email.setMsg(message);
            email.send();
        } catch (EmailException ex) {
            log.error("Erro no envio de email: " + ex.getMessage());
        }
    }

    public static void sendHtmlMail(String to, String subject, String message) {
        try {
            // Create a default MimeMessage object.
            MimeMessage mimeMessage = new MimeMessage(session);

            // Set From: header field of the header.
            mimeMessage.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            mimeMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: header field
            mimeMessage.setSubject(subject);

            // Send the actual HTML message, as big as you like
            mimeMessage.setContent(message,
                    "text/html");

            // Send message
            Transport.send(mimeMessage);

        } catch (MessagingException mex) {
            log.error("Erro no envio de email: " + mex.getMessage());
        }
    }

    // Utils
    public static void setupConfig(String path) {
        // Carregando as configurações a partir do arquivo
        try {
            InputStream resourceStream = MailHelper.class.getClassLoader().getResourceAsStream(path);
            props.load(resourceStream);
        } catch (Exception e) {
            String x = e.getMessage();
        }
    }
}
